package com.mr.cai.main.activity;

import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.mr.cai.main.R;
import com.mr.cai.main.adapter.ClassifyFgContentAdapter;
import com.mr.cai.main.fragment.ClassifyTitleFragment;
import com.mr.cai.main.utils.CommonCoreUtil;
import com.mr.cai.main.view.ContentViewPager;
import com.mr.cai.main.view.HorizontallyView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 16-2-25.
 */
public class ClassifyActivity extends TopViewBaseActivity implements HorizontallyView.onSelecterListener {
    private ImageView leftBackView;
    private EditText mEditText;
    private ContentViewPager mViewPager;
    private ClassifyFgContentAdapter mAdapter;
    private List testList=new ArrayList();
    private ViewPager.OnPageChangeListener fragment;
    @Override
    protected boolean isAttach() {
        return false;
    }

//    public static void launch(Activity context){
//        Intent intent=new Intent(context,ClassifyActivity.class);
//        context.startActivity(intent);
//    }


    @Override
    protected void setTopView() {
        super.setTopView();
        topRootView.setVisibility(View.GONE);
    }

    @Override
    protected View getContentView() {
        mPublicLayout= CommonCoreUtil.createPage(this, R.layout.classify_activity_layout);
        findView();
        return mPublicLayout;
    }

    private void findView() {
        fragment= (ClassifyTitleFragment) getSupportFragmentManager().findFragmentById(R.id.classifyTitleFg);
        leftBackView= (ImageView) mPublicLayout.findViewById(R.id.leftBackView);
        mEditText=(EditText)mPublicLayout.findViewById(R.id.mr_cai_search);
        mViewPager=(ContentViewPager)mPublicLayout.findViewById(R.id.classifyPager);
        mAdapter=new ClassifyFgContentAdapter(getSupportFragmentManager());
        mViewPager.setAdapter(mAdapter);
        mViewPager.setOffscreenPageLimit(2);
        testList.add("TEST");
        testList.add("TEST");
        testList.add("TEST");
        testList.add("TEST");
        testList.add("TEST");
        testList.add("TEST");
        testList.add("TEST");

        testList.add("TEST");
        testList.add("TEST");
        testList.add("TEST");
        testList.add("TEST");
        testList.add("TEST");
        testList.add("TEST");
        testList.add("TEST");
        mAdapter.setList(testList);

    }

    @Override
    protected void setContentListener() {
        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                if(fragment!=null){
                    fragment.onPageScrolled(position,positionOffset,positionOffsetPixels);
                }
            }

            @Override
            public void onPageSelected(int position) {
                if(fragment!=null){
                    fragment.onPageSelected(position);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                if(fragment!=null){
                    fragment.onPageScrollStateChanged(state);
                }
            }
        });
        leftBackView.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.leftBackView:
                destoryData();
                finish();
                break;

        }
    }

    @Override
    protected void destoryData() {
        mAdapter.clearData();
        mAdapter=null;
        mViewPager=null;
    }

    @Override
    public void onSelect(int index) {
            mViewPager.setCurrentItem(index);
    }
}
